$(document).ready(function () {
    dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: appBosch.crud.read,
                dataType: "json"
            },
            parameterMap: function (options, operation) {
                var params = {};
                switch (operation)
                {
                    case 'read':
                        break;
                }
                return params;
            }
        }
    });

    $("#grid").kendoGrid({
        scrollable: true,
        filterable: true,
        selectable: true,
        navigatable: true,
        resizable: true,
        columnMenu: true,
        sortable: true,
        dataBound: gridDataBound,
        dataSource: dataSource,
        schema: {
            model: {
                id: "value",
                fields: {
                    start: {type: 'date'},
                    end: {type: 'date'}
                }
            }
        },
        columns: [
            {
                title: "#",
                template: '#=appBosch.iTrainings++#',
                width:50
            },
            {
                field: "line", title: "Línea",
                width: 100
            },
            {
                field: "workstation", 
                title: "Estación",
                template: '<span style="text-decoration:underline;" data-id="#=value#" class="certdet">#=workstation#</span>'
            },
            {
                field: "start", 
                title: "Inicio del proceso", 
                template: '#=kendo.toString(new Date(start.replace(/-/g, "/")), "D")#'
            },
            {
                field: "end", 
                title: "Fin del proceso",
                template: '#=kendo.toString(new Date(end.replace(/-/g, "/")), "D")#'
            },
            {
                field: "ws_id", 
                title: " ",
                template: '<input type="hidden" name="workstation_id" value="#=ws_id#">',
                hidden: true
            },
            {
                command: [
                    {name: "startExam", text: "Iniciar certificación en piso", click: startExam}
                    ], 
                title: "Opciones"
            }
        ]
    });
});
function gridDataBound() {
    appBosch.iTrainings=1;
    $("#grid").kendoTooltip({
        filter: "span.certdet",
        content: {
            url: appBosch.urlDetails
        },
        width: 400,
        position: "top",
        requestStart: function(e) {
            e.options.url = appBosch.urlDetails+'/'+e.target.data("id");
        }
    });
}
function startExam(e)
{
    e.preventDefault();
    var url = appBosch.urlStartExam + '/index/' + $(e.currentTarget).closest("tr").find('td input[type=hidden]').val();
    $(location).attr('href', url);
}
